package com.woniu.user.controller;

import com.woniu.user.Es.EsModel.EsCoachInfo;
import com.woniu.user.Es.EsRepo.EsCoachRepo;
import com.woniu.user.commons.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.analysis.miscellaneous.KeepWordFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class SearchController {
    @Resource
    private EsCoachRepo esCoachRepo;

    @GetMapping("findAllCoach")
    public Result findAll(Integer page, Integer size) {
        log.info("查询所有教练");
        PageRequest pageRequest = PageRequest.of(page == null ? 0 : page-1, size == null ? 3 : size);
        Page<EsCoachInfo> all = esCoachRepo.findAll(pageRequest);
        System.out.println(all);
        return Result.success(200, "查询成功", all);
    }

    @GetMapping("/coachQuery")
    public Result query(String keyword, Integer page, Integer size) {
        log.info("{}条件查询", keyword);
        PageRequest pageRequest = PageRequest.of(page == null ? 0 : page-1, size == null ? 3 : size);
        return Result.success(200, "查询成功", esCoachRepo.search(keyword, pageRequest));
    }

    @GetMapping("/getOne")
    public Result findOne(Integer coachId) {
        log.info("查询coachId为{}的教练", coachId);
        return Result.success(200, "查询成功", esCoachRepo.findOne(coachId));
    }


}
